package LeetCode._02_数组;

public class _1109_航班预订统计 {
    public int[] corpFlightBookings(int[][] bookings, int n) {
        if(bookings == null || bookings.length == 0 || bookings[0].length == 0) return new int[0];
        int[] diff = new int[n];

        for (int[] booking : bookings) {
            //每一行都是一个一维数组且只有3个元素
            //题目索引从1开始，所以取值以后要减一
            //取范围[i,j]
            int i = booking[0] - 1;
            int j = booking[1] - 1;
            int val = booking[2];
            diff[i] += val;
            if(j+1<diff.length){
                diff[j+1] -= val;
            }
        }

        int[] res = new int[diff.length];
        res[0] = diff[0];
        //diff[i] = res[i] - res[i-1];
        for(int i=1;i<diff.length;i++){
            res[i] = res[i-1] + diff[i];
        }
        return res;
    }
}
